package rmi_impl;

import gui.GUIMain;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.security.auth.login.LoginException;

import rmi.Login;
import rmi.Session;
import structure.Course;
import structure.Exam;
import structure.Student;

public class LoginClass implements Login{
	
	private LinkedHashMap<Course, Exam> arrayCourse;
	private ArrayList<SessionClass> onlineStudents;
	
	public LoginClass() throws RemoteException {
		arrayCourse = new LinkedHashMap<>();
		onlineStudents = new ArrayList<>();
	}
	
	public boolean cancelable(){
		if(arrayCourse.size() == 0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean requestMarking(int sID){
		for(int i=0; i<onlineStudents.size();i++){
			if(sID == onlineStudents.get(i).getStudent().getiD()){
				try {
					onlineStudents.get(i).requestMarking();
					return true;
				} catch (RemoteException | SQLException e) {
					e.printStackTrace();
					return false;
				}
			}
		}
		return false;
	}
	
	public void clear(){
		for(int i=0; i<onlineStudents.size();i++){
			try {
				onlineStudents.get(i).logout();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		arrayCourse.clear();
	}
	
	public void addNewCourse(Course newCourse, Exam exam){
		this.arrayCourse.put(newCourse, exam);
		for(int i = 0; i < onlineStudents.size(); i++){
			if(onlineStudents.get(i).getStudent().getJoinedCourse() == newCourse.getCourseID()){
				onlineStudents.get(i).setExam(exam);
				onlineStudents.get(i).setCanDoExam(true);
			}
		}
	}
	
	public void removeCourse(int courseID){
		for(int i = 0; i < onlineStudents.size(); i++){
			if(onlineStudents.get(i).getStudent().getJoinedCourse() == courseID){
				onlineStudents.get(i).setCanDoExam(false);
			}
		}
		for(Entry<Course, Exam> entry : arrayCourse.entrySet()){
			if(entry.getKey().getCourseID() == courseID){
				arrayCourse.remove(entry.getKey());
				return;
			}
		}
	}
	
	public LinkedHashMap<Course, Exam> getCourse(){
		return arrayCourse;
	}

	@Override
	public Session login(String username, char[] password)
			throws RemoteException, LoginException, AlreadyBoundException {
		Student temp = checkLogin(username, password);
		if(temp!= null){
			for(SessionClass count : onlineStudents){
				if(count.getSID()== temp.getiD()){
					throw new AlreadyBoundException();
				}
			}
			SessionClass a = new SessionClass(temp,this);
			for(Entry<Course, Exam> entry : arrayCourse.entrySet()){
				if(entry.getKey().getCourseID() == a.getStudent().getJoinedCourse()){
					a.setExam(entry.getValue());
					a.setCanDoExam(true);
				}
			}
			onlineStudents.add(a);
			return onlineStudents.get(onlineStudents.indexOf((SessionClass)a));
		}else{
			return null;
		}
	}
	
	private Student checkLogin(String username, char[] pass){
		try {
			int sID = GUIMain.getDb().checkLogin(username, String.valueOf(pass));
			if(sID ==-1){
				return null;
			}
			return GUIMain.getDb().getStudentsByID(sID);
		} catch (SQLException e) {
		}
		return null;
	}
	
	public void removeStudent(SessionClass student){
		onlineStudents.remove(student);
	}
}
